package com.hippo.core.ui.compose.mine

import android.app.Application.MODE_PRIVATE
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hippo.core.R
import com.hippo.core.config.AppStoreKey
import com.hippo.core.config.LanguageCode
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.core.manager.AppStore
import com.hippo.core.ui.components.TextCheckLayout
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.TitleBar
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.utils.LanguageUtil

@Composable
fun LanguageScreen(onBack:()->Unit) {

    val context = LocalContext.current

    var checkIndex by remember{ mutableStateOf(0) }

    val sharedPreferences = context.getSharedPreferences("Settings", MODE_PRIVATE)
    val languageCode = sharedPreferences.getString("language", LanguageCode.EN)

    var code by remember{ mutableStateOf(languageCode) }

    Column(modifier = Modifier.fillMaxSize()) {

        TitleBar(
            title = stringResource(R.string.language_0),
            onBack = onBack
        )

        Column(
            modifier = Modifier
                .fillMaxSize()
                .paddingBody()
        ) {

            TextCheckLayout(text = stringResource(R.string.language_1), leftPainter = painterResource(R.mipmap.lang_1), isChecked = checkIndex == 1){
                checkIndex = 1
                code = LanguageCode.EN
            }
            TextCheckLayout(text = stringResource(R.string.language_2), leftPainter = painterResource(R.mipmap.lang_2), isChecked = checkIndex == 2){
                checkIndex = 2
                code = LanguageCode.DE
            }
            TextCheckLayout(text = stringResource(R.string.language_3), leftPainter = painterResource(R.mipmap.lang_3), isChecked = checkIndex == 3){
                checkIndex = 3
                code = LanguageCode.ES
            }
            TextCheckLayout(text = stringResource(R.string.language_4), leftPainter = painterResource(R.mipmap.lang_4), isChecked = checkIndex == 4){
                checkIndex = 4
                code = LanguageCode.FR
            }
            TextCheckLayout(text = stringResource(R.string.language_5), leftPainter = painterResource(R.mipmap.lang_5), isChecked = checkIndex == 5){
                checkIndex = 5
                code = LanguageCode.IT
            }
            TextCheckLayout(text = stringResource(R.string.language_6), leftPainter = painterResource(R.mipmap.lang_6), isChecked = checkIndex == 6){
                checkIndex = 6
                code = LanguageCode.PT
            }

             VerticalSpacer(36.dp, modifier = Modifier.weight(3F))

             ThemeButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.save),
                onClick = {
                    LanguageUtil.setAppLocale(context, code!!)
                    AppStore.save(AppStoreKey.languageCode,checkIndex)
                    onBack()
                }
            )

            VerticalSpacer(36.dp, modifier = Modifier.weight(1F))
        }

    }

}


@Preview(showBackground = true)
@Composable
private fun SettingScreenPreview() {
    HippoTheme {
        LanguageScreen({})
    }
}